<?
    error_reporting(E_ALL);
    require_once('./classes/config.inc.php');
    require_once('./classes/database.inc.php');
    $db = new LoggerDatabase();

    $TruncateSQL = 'TRUNCATE TABLE `[PREF]lastvisit`';
    $result = $db->Query($TruncateSQL);
    $InsertSQL = 'INSERT INTO `[PREF]lastvisit` (`date`) VALUES(NOW())';
    $result = $db->Query($InsertSQL);

    $module = $_REQUEST['module'];
    $report = $_REQUEST['report'];
    $date1 = $_REQUEST['date1'];
    $date2 = $_REQUEST['date2'];

    if (!DateCheck($date1))
        exit;
    if (!DateCheck($date2))
        exit;

    $params = array();
    foreach ($_REQUEST as $name => $value)
    {
        if ($name != 'module' && $name != 'report' && $name != 'date1' && $name != 'date2')
            $params[$name] = $value;
    }

    if (!isset(LoggerConfig::$modules[$module]))
        exit;

    if (!isset(LoggerConfig::$modules[$module]['reports'][$report]))
        exit;

    $reportItem = LoggerConfig::$modules[$module]['reports'][$report];

    include_once(dirname(__FILE__) . $reportItem['src']);

    if (!class_exists($reportItem['class']))
        exit;
    $class = new $reportItem['class']($db);
    $xml = new DOMDocument('1.0', 'UTF-8');
    $root = $xml->createElement('root');
    $xml->appendChild($root);

    $reportResult = $class->GetReport($root, $date1, $date2, $params);
    if ($reportResult)
    {
        $dojo = $xml->createElement('dojo', LoggerConfig::$dojo);
        $root->appendChild($dojo);
        $date1XML = $xml->createElement('date1', $date1);
        $root->appendChild($date1XML);
        $date1HumanXML = $xml->createElement('date1Human', date('d.m.Y', strtotime($date1)));
        $root->appendChild($date1HumanXML);
        $date2XML = $xml->createElement('date2', $date2);
        $root->appendChild($date2XML);
        $date2HumanXML = $xml->createElement('date2Human', date('d.m.Y', strtotime($date2)));
        $root->appendChild($date2HumanXML);
        $moduleXML = $xml->createElement('module', $module);
        $root->appendChild($moduleXML);
        $reportXML = $xml->createElement('report', $report);
        $root->appendChild($reportXML);

        $xpath = new DOMXPath($xml);
        $entries = $xpath->query('//processing-instruction()');//[@type="text/xsl"]
        if ($entries->length == 0)
            exit;

        $pi = $entries->item(0);

        if (class_exists('XSLTProcessor'))
        {
            $piData = $pi->data;
            if (!preg_match('#href="(.*?)"#i', $piData, $match))
                exit;
            $xslPath = $match[1];
    
            $xsl = new DOMDocument;
            $xsl->load($xslPath);
    
            $proc = new XSLTProcessor;
            $proc->importStyleSheet($xsl);
    
            header('Content-type: text/html');
            echo $proc->transformToXML($xml);
            exit;
        }
        else
        {
            $piData = $pi->data;
            if (!preg_match('#href="(.*?)"#i', $piData, $match))
                exit;
            $xslPath = $match[1];
            $xslPath = str_replace(dirname(__FILE__), '', $xslPath);
            $xslPath = substr($xslPath, 1);
            $pi->data = 'type="text/xsl" href="'.$xslPath.'"';
    
            header('Content-type: text/xml');
            echo $xml->saveXML();
            exit;
        }
    }

    function DateCheck(&$date)
    {
        if (preg_match('/^(\d{1,2})\.(\d{1,2})\.(\d{4})$/', $date, $match))
        {
            $year   = intval($match[3]);
            $month  = intval($match[2]);
            $day    = intval($match[1]);
        }
        elseif (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $date, $match))
        {
            $year   = intval($match[1]);
            $month  = intval($match[2]);
            $day    = intval($match[3]);
        }
        else
            return false;

        if (!checkdate($month, $day, $year))
            return false;

        $date = vsprintf("%04d-%02d-%02d", array($year, $month, $day));
        return true;
    }
    